package com.arithmeticEveryDay.dFS;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

// 全排列，求n个元素的全排列，如1 2 3的全排列为 1 2 3 ； 1 3 2 ； 2 1 3 ； 2 3 1 ； 3 1 2 ； 3 2 1
public class Arrange {
    static InOut0 io = new InOut0();
    int n;
    int[] arr1;
    boolean[] visited;
    int[] arr2;

    Arrange(int n) {
        this.n = n;
        this.arr1 = new int[n];
        this.visited = new boolean[n];
        this.arr2 = new int[n];
        for (int i = 0; i < n; i++) {
            arr2[i] = i;
        }
    }
    
    /*
    
        0            1             2
       / \          / \           / \
      1   2        0   2         0   1
      |   |        |   |         |   |
      2   1        2   0         1   0
    
     */
    public void dfs(int step) {
        if (step == n) {
            for (int j = 0; j < n; j++) {
                io.out.print(arr1[j] + " ");
            }
            io.out.println();
            return;
        }
        for (int i = 0; i < n; i++) {
            if (!visited[i]) {
                arr1[step] = i;
                visited[i] = true;
                dfs(step + 1);
                visited[i] = false;
            }
        }
    }
    
    /*
    
    012
    012-->012
       -->021
          012(恢复，下同)
    012(再恢复，下同)
    102-->102
       -->120
          102
    012
    210-->210
       -->201
          210
    012
    
     */
    public void fullPermutation(int begin) {
        if (begin == n) {
            for (int j = 0; j < n; j++) {
                io.out.print(arr2[j] + " ");
            }
            io.out.println();
            return;
        }
        for (int i = begin; i < n; i++) {
            swap(begin, i);
            fullPermutation(begin + 1);
            // 恢复
            swap(begin, i);
        }
    }
    

    private void swap(int x, int y) {
        // TODO Auto-generated method stub
        int temp = arr2[x];
        arr2[x] = arr2[y];
        arr2[y] = temp;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int n = io.nextInt();
        Arrange a = new Arrange(n);
        a.dfs(0);
        a.fullPermutation(0);
        io.out.flush();
    }
}

class InOut0 {
    BufferedReader in;
    StringTokenizer tok;
    PrintWriter out;

    InOut0() {
        in = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(new OutputStreamWriter(System.out));
    }

    boolean hasNext() {
        while (tok == null || !tok.hasMoreElements()) {
            try {
                tok = new StringTokenizer(in.readLine());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                return false;
            }
        }
        return true;
    }

    String next() {
        if (hasNext()) {
            return tok.nextToken();
        }
        return null;
    }

    int nextInt() {
        return Integer.parseInt(next());
    }
}
